// by Xeno

#define __Trans(tkind) _trans = getNumber(configFile >> #CfgVehicles >> typeOf _vehicle >> #tkind)
private ["_vehicle", "_camotype", "_camo", "_i", "_disabled", "_trans", "_empty", "_outb", "_hasbox"];
if (!isServer) exitWith{};

_vec_array = [];
// First we loop through the vehicle list which was passed. Those are the special vehicles.
// We set up the vehicle variables of the vehicle. This is only done at server start.
// We also create a camo at the starting position which hides the vehicle.
// And we create a vehicle array that is used to respawn the vehicle.
{
	_vehicle = _x select 0;
	_number_v = _x select 1;
	_createcamo = True;
	If (count _x > 2) then { _createcamo = _x select 2; };
	_vec_array set [count _vec_array, [_vehicle,_number_v,position _vehicle,direction _vehicle,typeOf _vehicle, _createcamo]];
	
	_vehicle setVariable ["D_OUT_OF_SPACE", -1];
	// We set the type of the vehicle (the number) MHQ, Repair, etc. See init.sqf where vrespawn is called.
	_vehicle setVariable ["d_vec", _number_v, true];
	_vehicle setAmmoCargo 0;
	
	// MHQ only
	if (_number_v < 10 || (_number_v > 99 && _number_v < 110)) then {
		// Event handler to change the status of the MHQ when it dies from Deployed to not Deployed.
		_vehicle addeventhandler ["killed", {(_this select 0) setVariable ["D_MHQ_Deployed",false,true]}];
	};
	If (_createcamo) then {
		// adds a camo over the vehicle at the base
		_camotype = switch (getNumber (configFile >> "CfgVehicles" >> typeOf _vehicle >> "side")) do {
			case 1: {"Land_CamoNetB_NATO"};
			case 0: {"Land_CamoNetB_EAST"};
		};
		// Create the camo in the air.
		_pos_camo = position _vehicle;
		_pos_camo set [2,1000];
		_camo = createVehicle [_camotype, _pos_camo, [], 0, "NONE"];
		_camo setDir (direction _vehicle) + 180;
		_camo setPos position _vehicle;
		_vehicle setVariable ["d_camonet", _camo];
	};
} forEach _this;
_this = nil;

if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 (%4 vehicles)",diag_tickTime,"vrespawn2.sqf","Execution started", count _vec_array]; };

sleep 65;

while {true} do {
	sleep 8 + random 5;
	for "_i" from 0 to (count _vec_array - 1) do {
		_vec_a = _vec_array select _i;
		_vehicle = _vec_a select 0;
		
		_disabled = false;
		if (damage _vehicle > 0.9) then {
			_disabled = true;
		} else {
			__Trans(transportAmmo);
			if (_trans > 0) then {
				_vehicle setAmmoCargo 1;
			};
			__Trans(transportRepair);
			if (_trans > 0) then {
				_vehicle setRepairCargo 1;
			};
			__Trans(transportFuel);
			if (_trans > 0) then {
				_vehicle setFuelCargo 1;
			};
		};
		
		_empty = if ((_vehicle call XfGetAliveCrew) > 0) then {false} else {true};
		
		if (_empty && !_disabled && alive _vehicle && (_vehicle call XOutOfBounds)) then {
			_outb = _vehicle getVariable "D_OUT_OF_SPACE";
			if (_outb != -1) then {
				if (time > _outb) then {_disabled = true};
			} else {
				_vehicle setVariable ["D_OUT_OF_SPACE", time + 600];
			};
		} else {
			_vehicle setVariable ["D_OUT_OF_SPACE", -1];
		};
		
		sleep 0.01;

		_aliveve = alive _vehicle;
		
		if ((_disabled && _empty) || (_empty && !_aliveve)) then {
			diag_log Format["(%1)[%2]: %3 %4(%5)",diag_tickTime,"vespawn2.sqf","Respawn started for ", (_vec_a select 0),(_vec_a select 1)];
			_number_v = _vec_a select 1;
			_createcamo = true;
			If (count _vec_a > 5) then { _createcamo = _vec_a select 5; };
			_hasbox = [_vehicle, "d_ammobox", false] call XfGetVar;
			if (_hasbox) then {["ammo_boxes",([ammo_boxes,1] call XNETGetJIP) - 1] call XNetSetJIP};
			// MHQ Only
			if (_number_v < 10 || (_number_v > 99 && _number_v < 110)) then {
				// Delete the MHQ Deploy Camo if it exists
				_dhqcamo = [_vehicle, "D_MHQ_Camo", objNull] call XfGetVar;
				if (!isNull _dhqcamo) then {deleteVehicle _dhqcamo};
			};
			// Delete the base Camo if it exists
			_camo = _vehicle getVariable "d_camonet";
			if (!isNil "_camo") then {deleteVehicle _camo;_camo = objNull} else {_camo = objNull};
			sleep 0.1;
			deletevehicle _vehicle;
			sleep 0.5;
			_vehicle = objNull;
			_vehicle = createVehicle [(_vec_a select 4), (_vec_a select 2), [], 0, "NONE"];
			_vehicle setpos (_vec_a select 2);
			_vehicle setdir (_vec_a select 3);
			// MHQ Only: to remove the MHQ_Deployed status
			if (_number_v < 10 || (_number_v > 99 && _number_v < 110)) then {
				_vehicle addeventhandler ["killed", {(_this select 0) setVariable ["D_MHQ_Deployed",false,true]}];
			};
			_vec_a set [0, _vehicle];
			_vec_array set [_i, _vec_a];
			_vehicle setVariable ["D_OUT_OF_SPACE", -1];
			_vehicle setVariable ["d_vec", _number_v, true];
			_vehicle setAmmoCargo 0;
			// Calls the event which will make the vehicle the special type it is (MHQ, etc) on the client side only.
			// The underlying call is done to initvec.sqf
			["d_n_v", _vehicle] call XNetCallEvent;
			
			if (_createcamo) then {
				sleep (0.5 + random (0.543));
				if (isNull _camo) then {
					_camotype = switch (getNumber (configFile >> "CfgVehicles" >> typeOf _vehicle >> "side")) do {
						case 1: {"Land_CamoNetB_NATO"};
						case 0: {"Land_CamoNetB_EAST"};
					};
					// Create the camo in the air.
					_pos_camo = position _vehicle;
					_pos_camo set [2,1000];
					_camo = createVehicle [_camotype, _pos_camo, [], 0, "NONE"];
					_camo setPos position _vehicle;
					_camo setDir (direction _vehicle) + 180;
					_vehicle setVariable ["d_camonet", _camo];
				};
			};
		};
		sleep 8 + random 5;
	};
};